﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace xFeedService
{
    public partial class DataFeed : ServiceBase
    {
        public DataFeed()
        {
            InitializeComponent();
        }
        // Work every hour
        System.Timers.Timer tsequence = new System.Timers.Timer(3600000);

        protected override void OnStart(string[] args)
        {
            tsequence.AutoReset = true;
            tsequence.Elapsed+= new System.Timers.ElapsedEventHandler(CheckTimer);
            StartCrawl(null);
        }

        void StartCrawl(object e)
        {
            try
            {
                BSL.objConfigBehaviour obj = new xFeedService.BSL.objConfigBehaviour();
                DAL.objConfig[] configs = obj.getListConfigObjectInTime();
                foreach (DAL.objConfig c in configs)
                {
                    Lib.Crawler.LinkPatternCrawling lpc = new Lib.Crawler.LinkPatternCrawling(c.ConfigLinkEx, c.ConfigDataEx);
                    lpc.PagePerRequest = 50;
                    lpc.CrawlLinks(c.ConfigWebsite, string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["StorePath"]) ? "C:\\xFeed\\Data" : System.Configuration.ConfigurationManager.AppSettings["StorePath"]);
                }
            }
            catch (Exception ex) { System.IO.File.AppendAllText("C:\\xFeed\\err.log", ex.ToString()); }
        }

        void StartIntegrate(object e)
        {
            try
            {
            string tableToFeed = "";
            string filePath = "";
            string categoryColumnName = "";

            tableToFeed = System.Configuration.ConfigurationManager.AppSettings["SQLTable"];
            filePath = System.Configuration.ConfigurationManager.AppSettings["StorePath"];
            categoryColumnName = System.Configuration.ConfigurationManager.AppSettings["CateColName"];

            Lib.Integration.IntegrateAllXML.IntegrateXMLToSQL(string.IsNullOrEmpty(categoryColumnName) ? "CategoryID" : categoryColumnName, string.IsNullOrEmpty(tableToFeed) ? "News" : tableToFeed, System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnection"].ConnectionString, string.IsNullOrEmpty(filePath) ? "C:\\xDataFeed\\Data" : filePath);
            }
            catch (Exception ex) { System.IO.File.AppendAllText("C:\\xFeed\\err.log", ex.ToString()); }
        }

        void CheckTimer(object e, System.Timers.ElapsedEventArgs args)
        {
            System.Threading.Thread trd1 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(StartCrawl));
            trd1.Start();
            System.Threading.Thread trd2 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(StartIntegrate));
            trd2.Start();
            trd1.Join();
            trd2.Join();

            
        }

        protected override void OnStop()
        {

        }
    }
}
